package dataservice;

import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.rmi.CORBA.Tie;
import javax.swing.table.DefaultTableModel;

import useDataBaseService.UpdateTableImpli;
import Enum.ColumnName;
import dBInfo.GetTableLimitation;
import dBInfo.Limitation;
import dBInfo.UpdateLimitation;
import databaseService.PersonalDataService;
import dataserviceImp.PersonalInfoImp;
import nju.edu.super_scholar.enums.Achivement;
import nju.edu.super_scholar.enums.Gender;
import nju.edu.super_scholar.enums.School;
import nju.edu.super_scholar.vo.PersonalInfoVO;

public class PersonalInfoOp implements PersonalInfoImp {
	private PersonalInfoVO personalInfoVO;
	private PersonalDataService personalDataService;
	private ArrayList<ColumnName> list;

	public PersonalInfoOp(PersonalInfoVO personalInfoVO) {
		// TODO Auto-generated constructor stub
		this.personalInfoVO = personalInfoVO;
		personalDataService = new PersonalDataService();
		initialColumnList();
	}

	public PersonalInfoOp() {
		personalInfoVO = new PersonalInfoVO();
		personalDataService = new PersonalDataService();
		initialColumnList();
	}

	@Override
	public PersonalInfoVO checkMyInfo(String id) throws RemoteException {
		// TODO Auto-generated method stub

		Limitation limitation = new Limitation();
		limitation.setLimitation(ColumnName.id, id);
		GetTableLimitation getTableLimitation = new GetTableLimitation(
				limitation, list);
		DefaultTableModel tableModel = personalDataService
				.getTable(getTableLimitation);
		personalInfoVO.setId(id);
		personalInfoVO.setNickname(tableModel.getValueAt(0, 0).toString());
		personalInfoVO.setGender(Gender.valueOf(tableModel.getValueAt(0, 2)
				.toString()));

		personalInfoVO.setAchivement(Achivement.valueOf(tableModel.getValueAt(
				0, 3).toString()));
		personalInfoVO.setLevel(Integer.parseInt(tableModel.getValueAt(0, 4)
				.toString()));
		personalInfoVO.setPetId(tableModel.getValueAt(0, 5).toString());

		personalInfoVO.setSchool(School.valueOf(tableModel.getValueAt(0, 6)
				.toString()));
		return personalInfoVO;
	}

	@Override
	public void modifyMyInfo(PersonalInfoVO personalInfoVO)
			throws RemoteException {
		// TODO Auto-generated method stub
		Limitation limitation=new Limitation();
		limitation.setLimitation(ColumnName.id, personalInfoVO.getId());
        limitation.setUpdate(ColumnName.name, personalInfoVO.getNickname());
        limitation.setUpdate(ColumnName.gender, personalInfoVO.getGender().toString());
        limitation.setUpdate(ColumnName.school, personalInfoVO.getSchool().toString());
	    UpdateLimitation updateLimitation=new UpdateLimitation(limitation);
        personalDataService.update(updateLimitation);
	
	}

	private void initialColumnList() {
		list = new ArrayList<ColumnName>();
		list.add(ColumnName.name);
		list.add(ColumnName.id);
		list.add(ColumnName.gender);
		list.add(ColumnName.title);
		list.add(ColumnName.rank);
		list.add(ColumnName.petid);
		list.add(ColumnName.school);
	}
}
